/*
 * @Author: 13938960424 1758142861@qq.com
 * @Date: 2023-11-05 16:59:32
 * @LastEditors: 13938960424 1758142861@qq.com
 * @LastEditTime: 2023-11-06 17:16:33
 * @FilePath: /sjf/new-xshell/muduo/test/echo.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "../server/muduo_server.hpp"
//#include "../server/m.hpp"

class EchoServer
{
private:
    TcpServer _server;

private:
    void OnConnected(const PtrConnection &conn)
    {
        DBG_LOG("NEW CONNECTION:%p", conn.get());
        //logMessage(DEBUG,"NEW CONNECTION:%p", conn.get());
    }
    void OnClosed(const PtrConnection &conn)
    {
        DBG_LOG("CLOSE CONNECTION:%p", conn.get());
        //logMessage(DEBUG,"CLOSE CONNECTION:%p", conn.get());
    }
    void OnMessage(const PtrConnection &conn, Buffer *buf)
    {
        conn->Send(buf->ReadPosition(), buf->ReadAblesSize() );
        buf->MoveReadOffset(buf->ReadAblesSize());
        conn->ShutDown();
    }

public:
    EchoServer(int port) : _server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this,std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected,this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this,std::placeholders::_1, std::placeholders::_2));
    }
    void Start() { _server.Start(); }
};
